package com.smartstate.IOC.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smartstate.IOC.dto.PageDTO;
import com.smartstate.IOC.dto.ReportDTO;
import com.smartstate.IOC.entity.Report;
import com.smartstate.IOC.mapper.CarbonConfigMapper;
import com.smartstate.IOC.mapper.DataMapper;
import com.smartstate.IOC.mapper.HistoryMapper;
import com.smartstate.IOC.service.HistoryService;
import com.smartstate.IOC.vo.DataVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class HistoryServiceImpl extends ServiceImpl<HistoryMapper, Report> implements HistoryService {

    @Resource
    private HistoryMapper historyMapper;

    @Override
    public Page<Report> queryAllPageByDTO(PageDTO pageDTO) {
        return null;
    }

    @Resource
    private DataMapper dataMapper;


    @Autowired
    CarbonConfigMapper carbonConfigMapper;




//    /**
//     * 当前时间的前N小时
//     */
//    public static String getBeforeHourTime(int ihour) {
//        String returnstr = "";
//        Calendar calendar = Calendar.getInstance();
//        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - ihour);
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        returnstr = sdf.format(calendar.getTime());
//        return returnstr;
//    }



    /**
     * 自定义查询
     * @return
     */
    @Override
    public Map<String, List<Report>> getHistoryCurveByDataDTO() {
        Map<String, List<Report>> map = new HashMap<>();
        ReportDTO reportDTO = new ReportDTO();
        String dateStr = new DateTime().toString().substring(0, 13);
        String todayDateStr = new DateTime().toDateStr();
        dateStr += ":00:00";
        todayDateStr += " 00:00:00";
        DateTime dateTime = DateUtil.parse(dateStr, "yyyy-MM-dd HH:mm:ss");
        DateTime todayDate = DateUtil.parse(todayDateStr, "yyyy-MM-dd HH:mm:ss");
        reportDTO.setDeviceID(carbonConfigMapper.getDeviceId());
//        判断表是否存在
        String objId = dataMapper.queryIdByDeviceId(reportDTO.getDeviceID());
        List<String> tableIsEmpty = historyMapper.queryTableIsEmpty(objId);
        if (tableIsEmpty == null){
            return null;
        }
//        查询7小时
        reportDTO.setStartTime(DateUtil.offsetHour(dateTime, -7).toString());
        reportDTO.setEndTime(dateStr);
        reportDTO.setType("hour");
        reportDTO.setTimeNumber("60");
        reportDTO = getTimeUnit(reportDTO);
        List<Report> hourList = check(reportDTO, objId);
        map.put("hour", hourList);
//        查询7天
        reportDTO.setStartTime(DateUtil.offsetDay(todayDate, -7).toString());
        reportDTO.setEndTime(todayDateStr);
        reportDTO.setType("day");
        reportDTO.setTimeNumber("3600");
        reportDTO = getTimeUnit(reportDTO);
        List<Report> dayList = check(reportDTO, objId);
        map.put("day", dayList);
        return map;
    }

    /**
     * 查询数据的逻辑处理
     * @param
     * @return
     */
    public List<Report> check(ReportDTO reportDTO, String objId){
        List<Report> list = null;
//            判断deviceId对应的表是否存在
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateTime dateTime = new DateTime();
        String nowDate = dateTime.toString();
        list = historyMapper.queryHistoryData(objId, reportDTO,nowDate);
        list = list.stream().map((report) -> {
            report.setDateTimeStr(simpleDateFormat.format(report.getDateTime()));
            return report;
        }).collect(Collectors.toList());
        return list;
    }

    /**
     * java反射bean的get方法
     *
     * @param
     * @param fieldName
     * @return
     */
    @SuppressWarnings("unchecked")
    public static Method getGetMethod(Report objectClass, String fieldName) {
        StringBuffer sb = new StringBuffer();
        sb.append("get");
        sb.append(fieldName.substring(0, 1).toUpperCase());
        sb.append(fieldName.substring(1));
        try {
            return objectClass.getClass().getMethod(sb.toString());
        } catch (Exception e) {
        }
        return null;
    }

    /**
     * 执行get方法
     *
     * @param o 执行对象
     * @param fieldName 属性
     */
    public static Object invokeGet(Report o, String fieldName) {
        Method method = getGetMethod(o, fieldName);
        if (method == null){
            return null;
        }
        try {
            return method.invoke(o, new Object[0]);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 时间换算
     * @param reportDTO
     * @return
     */
    public ReportDTO getTimeUnit(ReportDTO reportDTO){
        String timeNumber = reportDTO.getTimeNumber();
        switch (timeNumber){
            case "1" :
                reportDTO.setTimeUnit("s");
                break;
            case "60":
                reportDTO.setTimeUnit("m");
                break;
            case "3600":
                reportDTO.setTimeUnit("h");
                break;
            default:
                reportDTO.setTimeUnit("d");
        }
        reportDTO.setTimeNumber("1");
        return reportDTO;
    }
}
